package org.jeecg.modules.lab.items.temp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.cronJob.MyEnum;
import org.jeecg.common.cronJob.entity.LabCronJob;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.lab.cronJob.service.ILabCronJobService;
import org.jeecg.modules.lab.items.temp.entity.LabItemsTempSample;
import org.jeecg.modules.lab.items.temp.service.ILabItemsTempSampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;

/**
 * @Description: 实验项目模板_样品信息
 * @Author: jeecg-boot
 * @Date:   2023-10-08
 * @Version: V1.0
 */
@Api(tags="实验项目模板_样品信息")
@RestController
@RequestMapping("/temp/labItemsTempSample")
@Slf4j
public class LabItemsTempSampleController extends JeecgController<LabItemsTempSample, ILabItemsTempSampleService> {
	@Autowired
	private ILabItemsTempSampleService labItemsTempSampleService;

	@Autowired
	private ILabCronJobService labCronJobService;
	
	/**
	 * 分页列表查询
	 *
	 * @param labItemsTempSample
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "实验项目模板_样品信息-分页列表查询")
	@ApiOperation(value="实验项目模板_样品信息-分页列表查询", notes="实验项目模板_样品信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<LabItemsTempSample>> queryPageList(LabItemsTempSample labItemsTempSample,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LabItemsTempSample> queryWrapper = QueryGenerator.initQueryWrapper(labItemsTempSample, req.getParameterMap());
		Page<LabItemsTempSample> page = new Page<LabItemsTempSample>(pageNo, pageSize);
		IPage<LabItemsTempSample> pageList = labItemsTempSampleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param labItemsTempSample
	 * @return
	 */
	@AutoLog(value = "实验项目模板_样品信息-添加")
	@ApiOperation(value="实验项目模板_样品信息-添加", notes="实验项目模板_样品信息-添加")
	@RequiresPermissions("temp:lab_items_temp_sample:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody LabItemsTempSample labItemsTempSample) {
		labItemsTempSampleService.save(labItemsTempSample);

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp_sample","0",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param labItemsTempSample
	 * @return
	 */
	@AutoLog(value = "实验项目模板_样品信息-编辑")
	@ApiOperation(value="实验项目模板_样品信息-编辑", notes="实验项目模板_样品信息-编辑")
	@RequiresPermissions("temp:lab_items_temp_sample:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody LabItemsTempSample labItemsTempSample) {
		labItemsTempSample.setSynFlag("1");
		labItemsTempSampleService.updateById(labItemsTempSample);

		if (MyEnum.sync){
			long time = new Date().getTime();
			LabCronJob job = new LabCronJob("lab_items_temp_sample","1",time,1);
			labCronJobService.save(job);
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "实验项目模板_样品信息-通过id删除")
	@ApiOperation(value="实验项目模板_样品信息-通过id删除", notes="实验项目模板_样品信息-通过id删除")
	@RequiresPermissions("temp:lab_items_temp_sample:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		labItemsTempSampleService.removeById(id);

		if (MyEnum.sync){
			long time = new Date().getTime();
			String condition="id='"+id+"'";
			LabCronJob job = new LabCronJob("lab_items_temp_sample","2",condition,time,1);
			labCronJobService.save(job);
		}
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "实验项目模板_样品信息-批量删除")
	@ApiOperation(value="实验项目模板_样品信息-批量删除", notes="实验项目模板_样品信息-批量删除")
	@RequiresPermissions("temp:lab_items_temp_sample:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.labItemsTempSampleService.removeByIds(Arrays.asList(ids.split(",")));

		if (MyEnum.sync) {
			long time = new Date().getTime();
			String[] split = ids.split(",");
			labCronJobService.saveCronBatch("lab_items_temp_sample","2",time,split);
		}
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "实验项目模板_样品信息-通过id查询")
	@ApiOperation(value="实验项目模板_样品信息-通过id查询", notes="实验项目模板_样品信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<LabItemsTempSample> queryById(@RequestParam(name="id",required=true) String id) {
		LabItemsTempSample labItemsTempSample = labItemsTempSampleService.getById(id);
		if(labItemsTempSample==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(labItemsTempSample);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param labItemsTempSample
    */
    @RequiresPermissions("temp:lab_items_temp_sample:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LabItemsTempSample labItemsTempSample) {
        return super.exportXls(request, labItemsTempSample, LabItemsTempSample.class, "实验项目模板_样品信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("temp:lab_items_temp_sample:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LabItemsTempSample.class);
    }

}
